package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.AreaEffectCloudEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Box;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class MedicationSkill extends BaseSkill {

    public MedicationSkill(int level){super(level);}

    public MedicationSkill(NbtCompound nbtCompound){super(nbtCompound);}

    public MedicationSkill(){
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        return"每隔一段时间随机向附近友军脚下生成【速度】【力量】【抗性提升】【急迫】【防火】【伤害吸收】的药水云";
    }

    public SkillRarity getSkillRarity(){return SkillRarity.Rare;}
    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return mobEntity.world.getTime()>getSkilltime();
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if(canUseAbility(mobEntity)){
            int L = getSkillLevel() * 4;
            setSkilltime(mobEntity.world.getTime()+getCooldown()-getSkillLevel());
            Box box = mobEntity.getBoundingBox().stretch(L, L / 2, L).stretch(-L, -L / 2, -L);
            List<MobEntity> entityList = mobEntity.world.getEntitiesByClass(MobEntity.class,box,null);
            Iterator iterator1 = entityList.iterator();
            MobEntity friend = entityList.stream().findFirst().orElse(null);
            while (iterator1.hasNext())
            {
                if(friend.getTarget() != null && friend.getTarget() == mobEntity.getTarget())
                {
                    AreaEffectCloudEntity EffectCloud = EntityType.AREA_EFFECT_CLOUD.create(mobEntity.world);
                    Random random = new Random();
                    int num = random.nextInt(6);
                    switch (num)//选择6种正面效果之一
                    {
                        case 0:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.SPEED,100,getSkillLevel()));
                            break;
                        }
                        case 1:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.HASTE,100,getSkillLevel()));
                            break;
                        }
                        case 2:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.STRENGTH,100,getSkillLevel()));
                            break;
                        }
                        case 3:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.FIRE_RESISTANCE,100,getSkillLevel()));
                            break;
                        }
                        case 4:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.RESISTANCE,100,getSkillLevel()));
                            break;
                        }
                        case 5:
                        {
                            EffectCloud.addEffect(new StatusEffectInstance(StatusEffects.ABSORPTION,100,getSkillLevel()));
                            break;
                        }
                    }
                    EffectCloud.updatePosition(friend.getX(), friend.getY(), friend.getZ());
                    mobEntity.world.spawnEntity(EffectCloud);
                }
                friend = (MobEntity) iterator1.next();
            }
        }
        return true;
    }
}
